package cn.pku.sirx.snad.uias.dao.impl;

import java.util.Date;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import cn.pku.sirx.snad.ad.dao.impl.DaoImpl;
import cn.pku.sirx.snad.uias.dao.IMessageInfluenceDao;
import cn.pku.sirx.snad.uias.model.entity.MessageInfluence;

/**
 * @author WangXiao
 */
public class MessageInfluenceDaoImpl extends DaoImpl<MessageInfluence>
		implements IMessageInfluenceDao {

	public MessageInfluenceDaoImpl() {
		super(MessageInfluence.class);
	}

	@Override
	public double getInfluence(String fromUserId, String toUserId,
			Date fromDate, Date toDate) {
		DetachedCriteria criteria = DetachedCriteria
				.forClass(MessageInfluence.class);
		/*
		 * user limitation
		 */
		if (fromUserId != null) {
			criteria.add(Restrictions.eq("influencerId", fromUserId));
		}
		if (toUserId != null) {
			criteria.add(Restrictions.eq("influenceeId", toUserId));
		}
		/*
		 * date limitation
		 */
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}
		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}
		/*
		 * aggregation
		 */
		criteria.setProjection(Projections.sum("value"));
		Double result = (Double) getHibernateTemplate()
				.findByCriteria(criteria).get(0);
		return result == null ? .0 : result;
	}

}
